﻿using Dapper;
using Domain.Entity;
using Domain.Repository;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
    public class CMKRepository : ICMKRepository
    {
        private readonly MyDbContext myDbContext;

        public CMKRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        public async Task<int> AddCMK(CMK cMK)
        {
            myDbContext.CMK.Add(cMK);
            return await myDbContext.SaveChangesAsync();
        }

        public async Task<int> DeleteCMK(int id)
        {
            var CMK = await myDbContext.CMK.FindAsync(id);
            if (CMK != null)
            {
                myDbContext.CMK.Remove(CMK);
                return await myDbContext.SaveChangesAsync();
            }
            return 0;
        }

        public List<CMK> GetCMK()
        {
            using (SqlConnection connection = new SqlConnection("server=.;database=HR;user=sa;pwd=cssl#123;Encrypt=False;"))
            {
                string sql = "select * from dbo.CMK  ";
                return connection.Query<CMK>(sql).ToList();
            }
        }
    }
}
